package com.jdrx.sw.sewage.api.serve;

import com.jdrx.platform.commons.rest.beans.vo.ResponseVO;
import com.jdrx.platform.commons.rest.factory.ResponseFactory2;
import com.jdrx.sw.sewage.beans.dto.refreshDeviceCacheDTO;
import com.jdrx.sw.sewage.common.constant.Constant;
import com.jdrx.sw.sewage.service.GatherDevServiceImpl;
import com.jdrx.sw.sewage.service.common.CacheService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.IOException;

import static com.jdrx.sw.sewage.common.constant.Constant.TRANSPARENT_STATIONID_FEILD;

@RestController
@RequestMapping(value = "/api/collectiondata")
@Api(value = "采集数据初始化")
public class CollectionDataApi {

    @Autowired
    private GatherDevServiceImpl gatherDevServiceImpl;
    @Autowired
    private CacheService cacheService;

    /**
     * 数据检查
     */
    @ApiOperation(value = "数据检查")
    @RequestMapping(value = "dataCheck", method = RequestMethod.POST)
    public ResponseVO collectionDataCheck(@RequestHeader(Constant.TENANT_ID) String tenantId,
                                          MultipartFile file) throws IOException {
        return gatherDevServiceImpl.collectionDataCheck(file);
    }

    /**
     * 数据导入
     */
    @ApiOperation(value = "数据导入")
    @RequestMapping(value = "dataImport", method = RequestMethod.POST)
    public ResponseVO collectionDataImport(@RequestHeader(Constant.TENANT_ID) String tenantId,
                                           @RequestHeader(TRANSPARENT_STATIONID_FEILD) Long stationId,
                                           MultipartFile file) throws IOException {
        return gatherDevServiceImpl.collectionDataImport(file);
    }

    /**
     * mq设备缓存刷新
     */
    @ApiOperation(value = "mq/领用设备缓存刷新 - 【直接修改数据库点位信息或领用设备信息需手动刷新】")
    @RequestMapping(value = "refreshMQDeviceCache", method = RequestMethod.POST)
    public ResponseVO refreshMQDeviceCache(@RequestHeader(Constant.TENANT_ID) String tenantId,
                                           @RequestHeader(TRANSPARENT_STATIONID_FEILD) Long stationId,
                                           @RequestBody @Valid refreshDeviceCacheDTO dto) throws IOException {
        if (1 == dto.getCacheType()) cacheService.selectMQDevices();
        if (2 == dto.getCacheType()) cacheService.selectStorageByStationIds();
        return ResponseFactory2.ok();
    }
}
